package com.coltsmedia.RichmondHighNews;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.MenuItem;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;



public class MainMenuActivity extends SherlockActivity
{
    String welcome = "Slide to the right for more options.";

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);
        Toast.makeText(this, welcome, Toast.LENGTH_LONG).show();

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        ActionBar ab = getSupportActionBar();
        ab.setHomeButtonEnabled(true);
        ab.setDisplayHomeAsUpEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(R.string.app_name);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("Menu");
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        populateListView();

    }

    public void newsFeed(View view){
        Intent intent = new Intent(MainMenuActivity.this, ArticleListActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        if(CheckNetwork.isInternetAvailable(MainMenuActivity.this)) {
            startActivity(intent);
            overridePendingTransition(0,0);
        }
        else{
            Toast.makeText(MainMenuActivity.this, "No Internet Connection", 1000).show();
        }
    }

    public void weather(View view){
        Intent intent1 = new Intent(MainMenuActivity.this, WeatherActivity.class);
        if(CheckNetwork.isInternetAvailable(MainMenuActivity.this)) {
            startActivity(intent1);
        }
        else{
            Toast.makeText(MainMenuActivity.this, "No Internet Connection", 1000).show();
        }
    }

    public void schoolEvents(View view){
        Intent intent = new Intent(MainMenuActivity.this, SchoolEventsActivity.class);
        if(CheckNetwork.isInternetAvailable(MainMenuActivity.this)) {
            startActivity(intent);
        }
        else{
            Toast.makeText(MainMenuActivity.this, "No Internet Connection", 1000).show();
        }
    }

    public void coltZone(View view){
        Intent intent = new Intent(MainMenuActivity.this, ColtZoneFeatures.class);
        if(CheckNetwork.isInternetAvailable(MainMenuActivity.this)) {
            startActivity(intent);
        }
        else{
            Toast.makeText(MainMenuActivity.this, "No Internet Connection", 1000).show();
        }
    }


    private void populateListView() {

        String[] options = getResources().getStringArray(R.array.main_menu);
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.menu_list, options);
        final ListView list = (ListView) findViewById(R.id.left_drawer);
        list.setAdapter(arrayAdapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String url1 = "http://rhs.sd38.bc.ca/";
                String url2 = "https://twitter.com/RichmondColts";
                TextView text1 = (TextView) view.findViewById(R.id.menu_view);

                if (text1.getText().toString().equals("Richmond High School")) {
                    final Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(url1));
                    startActivity(intent1);
                }
                else if (text1.getText().toString().equals("Richmond High Twitter")) {
                    final Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(url2));
                    startActivity(intent2);
                }
                else if (text1.getText().toString().equals("Richmond High Facebook")) {
                    final Intent intent3 = getOpenFacebookIntent(MainMenuActivity.this);
                    startActivity(intent3);
                }

                else if (text1.getText().toString().equals("Credits and Licenses")) {
                    final Intent intent4 = new Intent(MainMenuActivity.this, CreditsActivity.class);
                    startActivity(intent4);
                }
            }

        });
    }

    public static Intent getOpenFacebookIntent(Context context) {

        try {
            context.getPackageManager().getPackageInfo("com.facebook.katana", 0);                       //Checks if FB is even installed.
            return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/391234834280206"));              //Tries to make intent with Facebook's URI
        } catch (Exception e) {
            return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/RichmondColts")); //Catches and opens a url to the desired page
        }
    }

    @Override
    protected void onPause(){
        super.onPause();
        overridePendingTransition(0,0);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }
}


